package main;

import edu.cn.nlsde.tmfst.model.Douban_topic;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.List;

/**
 * Created by dell on 2017/3/9.
 */
public class DouBanXiaoZu implements PageProcessor {
    private static Logger mylog = LoggerFactory.getLogger(DouBanXiaoZu.class.getName());
    private Site site = Site.me()
            .setSleepTime(10)
            .addHeader("Referer", "https://www.douban.com")
            .addHeader("Host", "www.douban.com")
            .addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8;charset=utf-8")
            .setUserAgent("Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36")
            .setCycleRetryTimes(2)
            .addCookie("123", "1234")
            .setRetrySleepTime(1000)
            .setTimeOut(1000);

    @Override
    public void process(Page page) {
        int now = Integer.parseInt(page.getUrl().regex("start=(\\d+)", 1).toString()) + 25;
        if (now < 750) {
            page.addTargetRequest(page.getUrl().replace("start=(\\d+)", "start=" + now).toString());
        }

        List<String> idlist = page.getHtml().regex("https://www.douban.com/group/topic/(\\d+)/", 1).all();
        for (String id : idlist) {
            Douban_topic one = new Douban_topic();
            one.topic_id = id;
            one.save_model();
            mylog.info(id);
        }
    }

    @Override
    public Site getSite() {
        return site;
    }

    public static void main(String[] args) {
        String[] urls = {"https://www.douban.com/group/550436/discussion?start=0",
                "https://www.douban.com/group/349665/discussion?start=0",
                "https://www.douban.com/group/549574/discussion?start=0",
                "https://www.douban.com/group/bjzf/discussion?start=0",
                "https://www.douban.com/group/xiaotanzi/discussion?start=0",
                "https://www.douban.com/group/bjzft/discussion?start=0",
                "https://www.douban.com/group/252218/discussion?start=0",
                "https://www.douban.com/group/sweethome/discussion?start=0",
                "https://www.douban.com/group/276176/discussion?start=0",
                "https://www.douban.com/group/opking/discussion?start=0",
                "https://www.douban.com/group/257523/discussion?start=0",
                "https://www.douban.com/group/zhufang/discussion?start=0",
                "https://www.douban.com/group/26926/discussion?start=0",
                "https://www.douban.com/group/279962/discussion?start=0",
                "https://www.douban.com/group/519274/discussion?start=0",
                "https://www.douban.com/group/fangzi/discussion?start=0",
                "https://www.douban.com/group/beijingzufang/discussion?start=0"};
        Spider.create(new DouBanXiaoZu())
                .addUrl(urls)
                .thread(1)
                .run();
    }
}
